/**
 * class  Radio
 * 单选框组件,实现方法和多选框类似
 * author haicent
 * email haicent@126.com
 * version 0.01
 */
package as3.component.controls
{
	import flash.display.Shape;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	
	import as3.component.support.BaseRadio;

	public class Radio extends BaseRadio
	{
		
		protected var _activeTextColor:uint;
		protected var _overTextColor:uint;
		protected var _enableTextColor:uint;
		protected var _fontSize:uint;
		protected var _fontBold:Boolean;
		
		protected var _textField:Label;
		protected var _label:String;
		
		private var _hitTestArea:Shape;
		private var _selectHitTestArea:Shape;
		
		protected var _defaultTextFormat:TextFormat;
		
		private var _space:Number = 4;
		
		public function Radio(labelValue:String = "单选框", dataValue:* = null, radioGroupValue:RadioGroup=null)
		{
			_label = labelValue || "单选框";
			super(radioGroupValue);
			value = dataValue;
		}
		
		override protected function initUI():void
		{
			super.initUI();
			
			_activeTextColor = _defaultSkin.radioCssTable.activeTextColor || _activeTextColor || 0x000000;
			_overTextColor = _defaultSkin.radioCssTable.overTextColor || _overTextColor || 0x000000;
			_enableTextColor = _defaultSkin.radioCssTable.enableTextColor || _enableTextColor || 0x666666;
			_fontSize = _defaultSkin.radioCssTable.fontSize || _fontSize || 12;
			_fontBold = _defaultSkin.radioCssTable.fontBold || _fontBold || false;
		}
		
		override protected function initCom():void
		{
			super.initCom();
			_defaultTextFormat =  new TextFormat(null, _fontSize, _activeTextColor, _fontBold);
			_textField = new Label();
			_textField.textField.defaultTextFormat = _defaultTextFormat;
			_textField.text = _label;
			_textField.selectable = false;
			addChild(_textField);
			_textField.mouseEnabled = false;
			_textField.mouseChildren = false;
		}
		
		override protected function creatUI():void
		{
			super.creatUI();
			
			_hitTestArea = new Shape();
			_selectHitTestArea = new Shape();
			reDrawHittestArea();
			
			_hitTestState = _hitTestArea;
			_selectHitTestState = _selectHitTestArea;
			
			updateEnable();
		}
		
		override protected function initListener():void
		{
			super.initListener();
			
			addEventListener(MouseEvent.ROLL_OVER, $onRollOverHandler);
			addEventListener(MouseEvent.ROLL_OUT, $onRollOutHandler);
		}
		
		private function reDrawHittestArea():void
		{
			
			var offy:Number = Math.ceil(( _textField.height - _simpleButton.height) * 0.5);
			_textField.x = _simpleButton.width + _space;
			_textField.y = _simpleButton.height > _textField.height ? offy : 0;
			_simpleButton.x = 0;
			_simpleButton.y = _simpleButton.height > _textField.height ? 0 : offy;
			
			_hitTestArea.graphics.clear();
			_hitTestArea.graphics.beginFill(0xffffff, 1);
			_hitTestArea.graphics.drawRect(0,0,_simpleButton.width + _textField.width, _simpleButton.height);
			_hitTestArea.graphics.endFill();
			
			_hitTestArea.graphics.beginFill(0xffffff, 1);
			_hitTestArea.graphics.drawRect(_textField.x,(_simpleButton.height - _textField.height)* 0.5,_textField.width, _textField.height + 1);
			_hitTestArea.graphics.endFill();
			
			_selectHitTestArea.graphics.clear();
			_selectHitTestArea.graphics.beginFill(0xffffff, 1);
			_selectHitTestArea.graphics.drawRect(0,0,_simpleButton.width + _textField.width, _simpleButton.height);
			_selectHitTestArea.graphics.endFill();
			
			_selectHitTestArea.graphics.beginFill(0xffffff, 1);
			_selectHitTestArea.graphics.drawRect(_textField.x,(_simpleButton.height - _textField.height)* 0.5,_textField.width, _textField.height + 1);
			_selectHitTestArea.graphics.endFill();
		}
		
		public function set label(value:String):void
		{
			if (value == _label) return;
			
			_label = value;
			_textField.text = value;
			reDrawHittestArea();
		}
		
		public function get label():String
		{
			return _textField.text;
		}
		
		override public function set width(value:Number) : void
		{
//			if (value <= _simpleButton.width)
//			{
//				_textField.width = 12;
//			} else {
//				if ((value - _simpleButton.width) >= _textField.width) return;
//				_textField.width = value - _simpleButton.width;
//			}
//			_textField.wordWrap = true;
//			_textField.adaptive = true;
//			reDrawHittestArea();
		}
		
		override public function get height() : Number
		{
			return _selectHitTestArea.height;
		}
		
		override protected function updateEnable():void
		{
			super.updateEnable();
			
			_defaultTextFormat.color = _enabled ? _activeTextColor : _enableTextColor;
			_textField.textField.defaultTextFormat = _defaultTextFormat;
			_textField.textField.text = _label;
		}
		
		private function $onRollOverHandler(evt:MouseEvent):void
		{
			if (enabled)
			{
				_defaultTextFormat.color = _overTextColor;
				_textField.textField.defaultTextFormat = _defaultTextFormat;
				_textField.textField.text = _label;
			}
		}
		
		private function $onRollOutHandler(evt:MouseEvent):void
		{
			if (enabled)
			{
				_defaultTextFormat.color = _activeTextColor;
				_textField.textField.defaultTextFormat = _defaultTextFormat;
				_textField.textField.text = _label;
			}
		}
		
	}
}